package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.beans.ArticleVO;
import com.icss.daos.ArticleDao;
import com.icss.daos.BoardDao;
import com.icss.daos.ClassDao;
import com.icss.daos.ReplyDao;
import com.icss.daos.bbsUserDao;

public class bbsDealManagerServlet extends HttpServlet {

	/**
	 * 处理manager.jsp传来的求情
	 * 管志春
	 * 2010-03-22
	 */
	public bbsDealManagerServlet() {
		super();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		doPost(request, response);
	}
	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		boolean flag;
		String choose = request.getParameter("choose");
		String cont = request.getParameter("cont");
		System.out.println(cont);
		/*添加操作*/
		
		//添加主板块
		if(choose.equals("addclass")){
			
			String sql = "insert into Class(className) values('"+cont+"')";
			System.out.println(sql);
			ClassDao classdao = new ClassDao();
			flag = classdao.executeSQL(sql);

			request.getRequestDispatcher("/BBS/manager.jsp").forward(request, response);
			classdao.close();
			
		}
		
		//添加二级版块
		if(choose.equals("addboard")){
			request.getRequestDispatcher("/BBS/addBoard.jsp").forward(request, response);
		}
		
		//添加会员
		if(choose.equals("adduser")){
			request.getRequestDispatcher("/BBS/addUser.jsp").forward(request, response);
		}
		
		//添加管理员
		if(choose.equals("addsuperManager")){
			
			String sql = "update BBSUser set pid=2 where userName='"+cont+"'";
			System.out.println(sql);
			bbsUserDao userDao = new bbsUserDao();
			flag = userDao.executeSQL(sql);
			
			request.getRequestDispatcher("/BBS/manager.jsp").forward(request, response);
			userDao.close();
		}
		
		/*修改操作*/
		
		
		//修改二级版块信息
		if(choose.equals("updateboard")){
			
			request.getRequestDispatcher("/BBS/updateBoard.jsp?boardName="+cont).forward(request, response);
		}
		
		//修改会员信息
		if(choose.equals("updateuser")){
			System.out.println(cont);
			//String useName = cont;
			request.getRequestDispatcher("/BBS/updateUserInfo.jsp?userName="+cont).forward(request, response);
		}

		/*删除操作*/
		
		//删除主模块
		if(choose.equals("deleteclass")){
			
			String sql = "delete Class where className='"+cont+"'";
			System.out.println(sql);
			ClassDao classdao = new ClassDao();
			flag = classdao.executeSQL(sql);

			request.getRequestDispatcher("/BBS/manager.jsp").forward(request, response);
			classdao.close();
		}
		
		//删除二级模块
		if(choose.equals("deleteboard")){
			
			String sql = "delete Board where boardName='"+cont+"'";
			BoardDao boardDao = new BoardDao();
			flag = boardDao.executeSQL(sql);
			if(flag){
				out.println("<script>alert('删除成功')</script>");
				
			}else{
				out.println("<script>alert('删除失败')</script>");
			}
			request.getRequestDispatcher("/BBS/manager.jsp").forward(request, response);
			boardDao.close();
		}
		
		//删除用户
		if(choose.equals("deleteuser")){
			
			String sql = "delete BBSUser where userName='"+cont+"'";
			bbsUserDao userDao = new bbsUserDao();
			flag = userDao.executeSQL(sql);
			if(flag){
				out.println("<script>alert('删除成功')</script>");
				
			}else{
				out.println("<script>alert('删除失败')</script>");
			}
			request.getRequestDispatcher("/BBS/manager.jsp").forward(request, response);
			userDao.close();
		}
		
		//取消管理员
		if(choose.equals("deletesuperManager")){
			
			String sql = "update BBSUser set pid=1 where userName ='"+cont+"'";
			bbsUserDao userDao = new bbsUserDao();
			flag = userDao.executeSQL(sql);
			if(flag){
				out.println("<script>alert('删除成功')</script>");
				
			}else{
				out.println("<script>alert('删除失败')</script>");
			}
			request.getRequestDispatcher("/BBS/manager.jsp").forward(request, response);
			userDao.close();
		}
		
		//删除文章
		if(choose.equals("deleteArticle")){
			
			ArticleDao articleDao = new ArticleDao();
			ReplyDao replyDao = new ReplyDao();
			articleDao.getArticleByName(cont);
			ArticleVO articleVO = new ArticleVO();
			int aid = articleVO.getAid();
			String sql1 = "delete Reply where aid="+aid;
			replyDao.executeSQL(sql1);
			
			String sql = "delete Article where aid="+aid;
			flag = articleDao.executeSQL(sql);
			if(flag){
				out.println("<script>alert('删除成功')</script>");
				
			}else{
				out.println("<script>alert('删除失败')</script>");
			}
			request.getRequestDispatcher("/BBS/manager.jsp").forward(request, response);
			replyDao.close();
			articleDao.close();
		}
		
		//删除回复
		if(choose.equals("deleteReply")){
			
			ReplyDao replyDao = new ReplyDao();
			String sql = "delete Reply where replyerName='"+cont+"'";
			flag = replyDao.executeSQL(sql);
			if(flag){
				out.println("<script>alert('删除成功')</script>");
				
			}else{
				out.println("<script>alert('删除失败')</script>");
			}
			request.getRequestDispatcher("/BBS/manager.jsp").forward(request, response);
		
			replyDao.close();
		}
		
		
	}
}
